package org.exez.modules.visitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.exez.common.system.vo.LoginUser;
import org.exez.modules.visitor.entity.VisitorCarAuthority;
import org.exez.modules.visitor.entity.WhiteList;
import org.exez.modules.visitor.mapper.VisitorCarAuthorityMapper;
import org.exez.modules.visitor.service.IVisitorCarAuthorityService;
import org.exez.modules.visitor.service.IWhiteListService;
import org.exez.modules.visitor.util.RequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 车辆限制
 * @Author: exez-boot
 * @Date: 2020-12-21
 * @Version: V1.0
 */
@Service
public class VisitorCarAuthorityServiceImpl extends ServiceImpl<VisitorCarAuthorityMapper, VisitorCarAuthority> implements IVisitorCarAuthorityService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveVisitorCarAuthority(String numberPlate, Integer id, int num) {
        if (StringUtils.isNotBlank(numberPlate)) {
            List<String> plateList = Arrays.asList(numberPlate.split(","));
            this.getVisitorCarAuthorities(id, plateList);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateVisitorCarAuthority(String numberPlate, Integer id) {
        if (StringUtils.isNotBlank(numberPlate)) {
            LambdaQueryWrapper<VisitorCarAuthority> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(VisitorCarAuthority::getCarBy, id);
            List<String> carList = this.list(wrapper).stream().map(VisitorCarAuthority::getName).collect(Collectors.toList());
            List<String> plateList = Arrays.asList(numberPlate.split(","));
            //增加的元素
            List<String> aList = RequestUtils.getAddaListThanbList(carList, plateList);
            //添加新数据
            this.getVisitorCarAuthorities(id, aList);
            //删除的元素
            List<String> bList = RequestUtils.getReduceaListThanbList(carList, plateList);
            if (bList.size()>0) {
                //删除不存在的数据
                LambdaQueryWrapper<VisitorCarAuthority> deleteWrapper = new LambdaQueryWrapper<>();
                deleteWrapper.eq(VisitorCarAuthority::getCarBy, id);
                deleteWrapper.in(VisitorCarAuthority::getName, bList);
                baseMapper.delete(deleteWrapper);
            }
        } else {
            LambdaQueryWrapper<VisitorCarAuthority> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(VisitorCarAuthority::getCarBy, id);
            this.remove(wrapper);
        }
    }

    private void getVisitorCarAuthorities(Integer id, List<String> aList) {
        List<VisitorCarAuthority> visitorCarAuthorityList = new ArrayList<>();
        VisitorCarAuthority visitorCarAuthority;
        for (String s : aList) {
            visitorCarAuthority = new VisitorCarAuthority();
            visitorCarAuthority.setCreateTime(new Date());
            visitorCarAuthority.setName(s);
            visitorCarAuthority.setCarBy(id);
            visitorCarAuthorityList.add(visitorCarAuthority);
        }
        //批量保存
        saveBatch(visitorCarAuthorityList);
    }

    public static void main(String[] args) {
        List<Integer> arr = new ArrayList<Integer>();

        List<Integer> a = Arrays.asList(1, 2, 3, 4, 6);
        List<Integer> b = Arrays.asList(4, 3, 2, 1, 9);

        //增加的元素组成的列表
        List<Integer> aList = RequestUtils.getAddaListThanbList(a, b);
        for (Integer integer : aList) {
            System.out.println("增加的元素：" + integer);
        }

        //减少的元素组成的列表
        List<Integer> bList = RequestUtils.getReduceaListThanbList(a, b);
        for (Integer integer : bList) {
            System.out.println("减少的元素：" + integer);
        }

        //封装
        arr.addAll(a);
        arr.addAll(b);
        arr.addAll(aList);
        arr.addAll(bList);


        //去重
        RequestUtils.cleanDisRepet(arr);

        for (Integer integer : arr) {
            System.out.println("全部元素：" + integer);
        }
    }


}
